﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.IO;
using System.Collections;
namespace PJOnimal
{
    public class MapGame : GameScene
    {
        List<String> sbuffer = new List<String>();         
        public ArrayList lstObj = new ArrayList();
        public ArrayList lstEnm = new ArrayList();
        int tocDoEnm = 1;
        public MapGame(Onimal game)
            : base(game)
        {
            hinhNen = game.Content.Load<Texture2D>("Map/gach2");
           
        }

        public List<String> ReadFile(string file)
        {
            List<String> buffer = new List<String>();
            StreamReader sr = File.OpenText(file);
            String str = "";

            while ((str = sr.ReadLine()) != null)
            {
                buffer.Add(str);
            }
            sr.Close();
            return buffer;
        }
        public void LoadMap(Onimal game)
        {            
            sbuffer = ReadFile ("Map1.txt");
            for (int i = 0; i < sbuffer.Count; i++)
            {
                Char[] chr = new Char[2];
                chr[0]=':';
                chr[1]=',';
                if (sbuffer[i] == "")
                {                    
                    continue;
                }
                String[] str = new String[3];
                
                str = sbuffer[i].Split(chr);
                switch (str[0])
                {
                    case "g1":
                        MapObj mObj = new MapObj(hinhNen, float.Parse(str[1]), float.Parse(str[2]));
                        mObj.Width = 50;
                        mObj.Height = 50;
                        lstObj.Add(mObj);
                        break;
                    case "b1":
                        Texture2D[] sprite = new Texture2D[9];
                        LoadResource(sprite, 9, "Enemy/Bat/");
                        Sprite spr = new Sprite(sprite, 5, new Vector2(float.Parse(str[1]), float.Parse(str[2])), 1);
                        spr.Width = 114;
                        spr.Height = 104;
                        spr.DHeightO = 24;
                        spr.DHeightU = 32;
                        spr.DWidthL  = 16;
                        spr.DWidthR = 16;
                        spr.IType = 30;
                        lstEnm.Add(spr);
                        break;
                    case "a1":
                        Texture2D[] sprite1 = new Texture2D[16];
                        LoadResource(sprite1, 16, "Enemy/Ailen/ailen-");
                        Sprite spr1 = new Sprite(sprite1, 4, new Vector2(float.Parse(str[1]), float.Parse(str[2])), 1);
                        spr1.Width = 133;
                        spr1.Height = 74;
                        spr1.DHeightO = 0;
                        spr1.DHeightU = 0;
                        spr1.DWidthL = 0;
                        spr1.DWidthR = 0;
                        spr1.IType = 40;
                        lstEnm.Add(spr1);
                        break;
                    case "z1":
                        Texture2D[] sprite2 = new Texture2D[14];
                        LoadResource(sprite2, 14, "Enemy/Zombie/zombie-");
                        Sprite spr2 = new Sprite(sprite2, 4, new Vector2(float.Parse(str[1]), float.Parse(str[2])), 1);
                        spr2.Width = 94;
                        spr2.Height = 98;
                        spr2.DHeightO = 24;
                        spr2.DHeightU = 24;
                        spr2.DWidthL = 8;
                        spr2.DWidthR = 8;
                        spr2.IType = 50;
                        lstEnm.Add(spr2);
                        break;
                    case "eb1":
                        Texture2D[] sprite3 = new Texture2D[11];
                        LoadResource(sprite3, 11, "Enemy/EvilBat/bat-");
                        Sprite spr3 = new Sprite(sprite3, 4, new Vector2(float.Parse(str[1]), float.Parse(str[2])), 1);
                        spr3.Width = 78;
                        spr3.Height = 100;
                        spr3.DHeightO = 8;
                        spr3.DHeightU = 48;
                        spr3.DWidthL = 16;
                        spr3.DWidthR = 16;
                        spr3.IType = 60;
                        lstEnm.Add(spr3);
                        break;
                }
                
            }

        }
        public void LoadResource(Texture2D[] sprite,int n,string name)
        {
            for (int i = 0; i < n; i++)
            {
                sprite[i] = game.Content.Load<Texture2D>(name + (i+1).ToString("00"));
            }
        }
        public void Update(float khoangCach)
        {
            for (int i = 0; i < lstObj.Count; i++)
            {
                ((MapObj)lstObj[i]).Update(khoangCach);
                
            }
            for (int i = 0; i < lstEnm.Count; i++)
            {
                ((Sprite)lstEnm[i]).UpdateEnemy(khoangCach);
            }
        }
        public void UpdateEnemy(GameTime gameTime,float x)
        {            
            for (int i = 0; i < lstEnm.Count; i++)
            {
                ((Sprite)lstEnm[i]).UpdateAnimationEnemy(gameTime,x);
            }

        }
        public void UpdateXYEnemy(GameTime gameTime,Character nhanvat)
        {            
            for (int i = 0; i < lstEnm.Count; i++)
            {
                if (((Sprite)lstEnm[i]).FlagAction == 11)
                {
                    ((Sprite)lstEnm[i]).Y += 3;
                    //if (((Sprite)lstEnm[i]).Y > 600)
                    //{
                    //    lstEnm.RemoveAt(i);
                    //    i--;
                    //}
                }
                Vector2 Enemy = new Vector2((float)((Sprite)lstEnm[i]).X, (float)((Sprite)lstEnm[i]).Y);
                float ftrai = nhanvat.mySprite.X - Enemy.X;
                float fphai = Enemy.X - nhanvat.mySprite.X;
                //if ((ftrai < 300 && ftrai > 0))
                //{                    
                //    if (nhanvat.mySprite.Y > Enemy.Y)
                //    {
                //        ((Sprite)lstEnm[i]).Y += 3;
                //    }
                //    if (nhanvat.mySprite.Y <= Enemy.Y)
                //    {
                //        ((Sprite)lstEnm[i]).X += 3;
                //        ((Sprite)lstEnm[i]).FlagMoveEnemy =1;
                //    }
                //}
               
                if ((fphai < 250 && fphai > 0) )
                {
                    if(nhanvat.mySprite.Y > Enemy.Y)
                        ((Sprite)lstEnm[i]).Y += 3;
                    if (nhanvat.mySprite.Y <= Enemy.Y)
                    {
                        ((Sprite)lstEnm[i]).UpdateDiChuyen(-tocDoEnm, 0);
                        ((Sprite)lstEnm[i]).FlagMoveEnemy = -1;
                    }
                }
                if(((Sprite)lstEnm[i]).FlagMoveEnemy == -1)
                    ((Sprite)lstEnm[i]).UpdateDiChuyen( - tocDoEnm,0);
                if (nhanvat.mySprite.X - Enemy.X > 450)
                {
                    lstEnm.RemoveAt (i);
                    i--;
                }
                if (nhanvat.mySprite.X - Enemy.X > 100 || Enemy.X - nhanvat.mySprite.X > 100)
                    continue;
                switch (nhanvat.DungCham(Enemy, new Vector2(((Sprite)lstEnm[i]).Width, ((Sprite)lstEnm[i]).Height)
                    , ((Sprite)lstEnm[i]).FlagAction ))
                {
                    case 1:
                        nhanvat.mySprite.FlagAction = 11;
                        break;
                    case 2:
                        ((Sprite)lstEnm[i]).FlagAction = 11;
                        break;
                }
            }

        }
        public void UpdateToaDoCu(float x,float y)
        {
            for (int i = 0; i < lstObj.Count; i++)
                ((MapObj)lstObj[i]).UpdateToaDoCu(x+i*50,y);
        }
        public override void Update(GameTime gameTime)
        {
            
        }
        public override void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < lstObj.Count; i++)
            {
                ((MapObj)lstObj[i]).Draw(spriteBatch);
                
            }
            for (int i = 0; i < lstEnm.Count; i++)
            {
                ((Sprite)lstEnm[i]).Draw(spriteBatch);

            }
            base.Draw(spriteBatch);
        }
    }
}
